package io.renren.modules.monitor.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.modules.monitor.dao.MonitorDeviceDao;
import io.renren.modules.monitor.dto.MonitorDeviceDTO;
import io.renren.modules.monitor.entity.MonitorDeviceEntity;
import io.renren.modules.monitor.service.MonitorDeviceService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.enums.SuperAdminEnum;
import io.renren.modules.sys.service.SysDeptService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class MonitorDeviceServiceImpl extends BaseServiceImpl<MonitorDeviceDao, MonitorDeviceEntity> implements MonitorDeviceService{

    private final SysDeptService sysDeptService;

    @Override
    public PageData<MonitorDeviceDTO> page(Map<String, Object> params) {
        //分页
        IPage<MonitorDeviceEntity> page = getPage(params, "", true);

        //管理员及普通用户，只能查询所属部门及子部门的数据
        UserDetail user = SecurityUser.getUser();
        if (user.getSuperAdmin() == SuperAdminEnum.NO.value()) {
            params.put("deptIdList",sysDeptService.getSubDeptIdList(user.getDeptId()));
        }

        //查询
        List<MonitorDeviceEntity> list = baseDao.getList(params);

        //返回监控设备信息
        return getPageData(list, page.getTotal(), MonitorDeviceDTO.class);
    }
}
